package com.iyoudoock.citymine.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.iyoudoock.citymine.BaseActivity;
import com.iyoudoock.citymine.R;
import com.iyoudoock.citymine.adapter.DistrictListAdapter;
import com.iyoudoock.citymine.bean.DistrictBean;
import com.iyoudoock.citymine.constants.HttpUtils;
import com.iyoudoock.citymine.constants.Key;
import com.iyoudoock.citymine.constants.Value;
import com.iyoudoock.citymine.utils.SystemUtils;
import com.kvin.toolkit.adapter.OnChildClickListener;
import com.kvin.toolkit.utils.ToastUtils;
import com.kvin.toolkit.utils.http.JsonUtils;
import com.kvin.toolkit.utils.system.AnimUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DistrictListActivity extends BaseActivity {

    @BindView(R.id.nav_back_iv)
    ImageView navBackIv;
    @BindView(R.id.nav_container)
    LinearLayout navContainer;
    @BindView(R.id.district_lv)
    RecyclerView districtLv;
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.nav_search_iv)
    ImageView navSearchIv;

    private DistrictListAdapter mAdapter;
    private ArrayList<DistrictBean> districts;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_district_list);
        ButterKnife.bind(this);
    }

    @Override
    public void initNav() {
        hideActionbar();
    }

    @Override
    public void init() {
        mJsonUtils = JsonUtils.getInstance();

        districts = new ArrayList<>();
        mAdapter = new DistrictListAdapter(districts, this);
        districtLv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        districtLv.setAdapter(mAdapter);
        mAdapter.setOnChildClickListener(mOnChildClickListener);

//        searchEt.setOnKeyListener(mOnKeyListener);
        searchEt.setOnEditorActionListener(mOnEditorActionListener);

        this.navBackIv.setOnClickListener(this);
        this.navSearchIv.setOnClickListener(this);
    }

    @Override
    public void loadingData() {
        if (districts.size() > 0) districts.clear();
        String address = getIntent().getStringExtra(Key.User.ADDRESS);
        String[] add = SystemUtils.convertToArray(address);
        String keyWord = searchEt.getText().toString();
        if (TextUtils.isEmpty(keyWord)) {
            ToastUtils.simpleToast(getActivity(), R.string.tip_keyowrd_null);
            return;
        }
        if (add.length < 3) return;
        HashMap<String, String> params = HttpUtils.getDistrictInfo(add[0], add[1], add[2], keyWord);
        mJsonUtils.post(HttpUtils.DISTRICT_INFO_URL, params, this, Value.Flag.DISTRICT_INFO);
    }


    private TextView.OnEditorActionListener mOnEditorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                loadingData();
                com.kvin.toolkit.utils.system.SystemUtils.closeKeyboard(getActivity());
                return true;
            }
            return false;
        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nav_back_iv:
                this.finish();
                break;
            case R.id.nav_search_iv:
                loadingData();
                com.kvin.toolkit.utils.system.SystemUtils.closeKeyboard(getActivity());
                break;
        }
    }

    /**
     * on item click event
     */
    private OnChildClickListener mOnChildClickListener = new OnChildClickListener() {
        @Override
        public void onClick(View view, int p) {
            DistrictBean b = districts.get(p);
            if (b.getId()==1){
                ToastUtils.simpleToast(getActivity(),"服务未开通，请重新选择区域");
                return;
            }
            mIntent = new Intent();
            mIntent.putExtra(Key.User.DISTRICT, b.getVillageName());
            mIntent.putExtra(Key.User.DISTRICT_ID, b.getId());
            setResult(RESULT_OK, mIntent);
            finish();
            AnimUtils.setAnim(getActivity(), AnimUtils.AnimStyle.LEFT_IN_FADE_OUT);
        }
    };

    @Override
    public void onSuccess(JSONObject response, String flag) {
        super.onSuccess(response, flag);
        if (response == null) return;
        try {
            String s = response.getString(Key.Tag.DATA);
            if (flag.equals(Value.Flag.DISTRICT_INFO)) {
                List<DistrictBean> list = JSON.parseArray(s, DistrictBean.class);
                if (list == null || list.size() < 1) {
                    DistrictBean bean = new DistrictBean();
                    bean.setVillageName("暂无服务");
                    bean.setId(-1);
                    districts.add(bean);
                } else {
                    districts.addAll(list);
                }
                mAdapter.notifyDataSetChanged();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(IOException e, String flag) {
        super.onFailure(e, flag);
    }
}
